package com.example.messaging;

import java.util.LinkedHashMap;
import java.util.Map;

import com.example.sensors.Sensor_Accelometer;
import com.example.sensors.Sensor_Audio;
import com.example.sensors.Sensor_Battery;
import com.example.sensors.Sensor_GPS;
import com.example.sensors.Sensor_PhoneLock;
import com.example.sensors.Sensor_Phone_Calls;
import com.example.sensors.Sensor_Power;
import com.example.sensors.Sensor_Screen;
import com.example.sensors.Sensor_Transport_Mode;
import com.example.sensors.Sensor_Wired_Headset;
import com.example.sensors.Sensor_running_applications;

public class MessageFactory {
	private static MessageFactory instance;

	public static MessageFactory getInstance() {
		if (instance == null)
			instance = new MessageFactory();
		return instance;
	}

	public int getParameterNumber(String sensorName, String parameterName) {
		new LinkedHashMap<String, Object>();
		int parameterNumber = 1;
		boolean found = false;
		if (!found && parameterName.equals("class"))
			found = true;
		else
			parameterNumber++;
		if (!found && parameterName.equals("sensorName"))
			found = true;
		else
			parameterNumber++;
		if (!found && parameterName.equals("eventType"))
			found = true;
		else
			parameterNumber++;
		if (!found && parameterName.equals("sensorFunctionality")) {
			found = true;
		}
		if (!found && Sensor_Accelometer.NAME.equals(sensorName)) {
			if (!found && parameterName.equals("sensorEvent"))
				found = true;
		}
		if (!found && Sensor_GPS.NAME.equals(sensorName)) {
			if (!found && parameterName.equals("location"))
				found = true;
		}
		if (!found && Sensor_Audio.NAME.equals(sensorName)) {
			if (!found && parameterName.equals("sensorType"))
				found = true;
			else
				parameterNumber++;
			if (!found && parameterName.equals("volume"))
				found = true;
		}
		if (!found && Sensor_Battery.NAME.equals(sensorName)) {
			if (!found && parameterName.equals("batteryLevel"))
				found = true;
		}
		if (!found && Sensor_Phone_Calls.NAME.equals(sensorName)) {
			if (!found && parameterName.equals("phoneNumber"))
				found = true;
			else
				parameterNumber++;
			if (!found && parameterName.equals("callDir"))
				found = true;
		}
		if (!found && Sensor_PhoneLock.NAME.equals(sensorName)) {
			if (!found && parameterName.equals("phoneLock"))
				found = true;
		}
		if (!found && Sensor_Power.NAME.equals(sensorName)) {
			if (!found && parameterName.equals("status"))
				found = true;
			else
				parameterNumber++;
			if (!found && parameterName.equals("plugType"))
				found = true;
		}
		if (!found && Sensor_running_applications.NAME.equals(sensorName)) {
			if (!found && parameterName.equals("processName"))
				found = true;
			else
				parameterNumber++;
			if (!found && parameterName.equals("processActivity"))
				found = true;
		}
		if (!found && Sensor_Screen.NAME.equals(sensorName)) {
			if (!found && parameterName.equals("screenActivity"))
				found = true;
			else
				parameterNumber++;
			if (!found && parameterName.equals("userActivity"))
				found = true;
		}
		if (!found && Sensor_Transport_Mode.NAME.equals(sensorName)) {
			if (!found && parameterName.equals("transportMode"))
				found = true;
		}
		if (!found && Sensor_Wired_Headset.NAME.equals(sensorName)) {
			if (!found && parameterName.equals("wiredHeadsetActivity"))
				found = true;
		}
		return parameterNumber;
	}

	public static Map<String, Object> createMessageForClass(Class<?> theClass,
			String messageType) {
		Map<String, Object> returnVal = new LinkedHashMap<String, Object>();
		int parameterNumber = 1;
		returnVal.put("class", parameterNumber++);
		returnVal.put("sensorName", parameterNumber++);
		returnVal.put("eventType", parameterNumber++);
		if (messageType.equals("sensorActivityChange")) {
			returnVal.put("sensorFunctionality", parameterNumber++);
		}
		if (Sensor_Accelometer.class.isAssignableFrom(theClass)
				&& messageType.equals("sensorInfoReceived")) {
			returnVal.put("sensorEvent", parameterNumber++);
		}
		if (Sensor_GPS.class.isAssignableFrom(theClass)
				&& messageType.equals("sensorInfoReceived")) {
			returnVal.put("location", parameterNumber++);
		}
		if (Sensor_Audio.class.isAssignableFrom(theClass)
				&& messageType.equals("sensorInfoReceived")) {
			returnVal.put("streamType", parameterNumber++);
			returnVal.put("volume", parameterNumber++);
		}
		if (Sensor_Battery.class.isAssignableFrom(theClass)
				&& messageType.equals("sensorInfoReceived")) {
			returnVal.put("batteryLevel", parameterNumber++);
		}
		if (Sensor_Phone_Calls.class.isAssignableFrom(theClass)
				&& messageType.equals("sensorInfoReceived")) {
			returnVal.put("phoneNumber", parameterNumber++);
			returnVal.put("callDir", parameterNumber++);
		}
		if (Sensor_PhoneLock.class.isAssignableFrom(theClass)
				&& messageType.equals("sensorInfoReceived")) {
			returnVal.put("phoneLock", parameterNumber++);
		}
		if (Sensor_Power.class.isAssignableFrom(theClass)
				&& messageType.equals("sensorInfoReceived")) {
			returnVal.put("status", parameterNumber++);
			returnVal.put("plugType", parameterNumber++);
		}
		if (Sensor_running_applications.class.isAssignableFrom(theClass)
				&& messageType.equals("sensorInfoReceived")) {
			returnVal.put("processName", parameterNumber++);
			returnVal.put("processActivity", parameterNumber++);
		}
		if (Sensor_Screen.class.isAssignableFrom(theClass)
				&& messageType.equals("sensorInfoReceived")) {
			returnVal.put("screenActivity", parameterNumber++);
			returnVal.put("userActivity", parameterNumber++);
		}
		if (Sensor_Transport_Mode.class.isAssignableFrom(theClass)
				&& messageType.equals("sensorInfoReceived")) {
			returnVal.put("transportMode", parameterNumber++);
		}
		if (Sensor_Wired_Headset.class.isAssignableFrom(theClass)
				&& messageType.equals("sensorInfoReceived")) {
			returnVal.put("wiredHeadsetActivity", parameterNumber++);
		}
		return returnVal;
	}
}
